// TrainPropertySheet.cpp : implementation file
//

#include "stdafx.h"
#include "ConfigGUI.h"
#include "TrainPropertySheet.h"
#include "TrainDlg.h"
#include "..\GUISys\storeconfig.h"
#include "global.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

extern guiimport_t g_gui_imp;
extern pfnSetShooter2 g_set_shooter2;

/////////////////////////////////////////////////////////////////////////////
// CTrainPropertySheet

IMPLEMENT_DYNAMIC(CTrainPropertySheet, CPropertySheet)

void CTrainPropertySheet::InitPropPages(void)
{
	m_ppTech = new CTechPropertyPage;
	m_ppTech->SetPropSheet(this);
	m_ppShootSet = new CShootSetPropertyPage;
	m_ppShootSet->SetPropSheet(this);
	AddPage(m_ppTech);
	AddPage(m_ppShootSet);
}

CTrainPropertySheet::CTrainPropertySheet() : CPropertySheet()
{
	InitPropPages();
}

CTrainPropertySheet::CTrainPropertySheet(UINT nIDCaption, CWnd* pParentWnd, UINT iSelectPage)
	:CPropertySheet(nIDCaption, pParentWnd, iSelectPage)
{
	InitPropPages();
}

CTrainPropertySheet::CTrainPropertySheet(LPCTSTR pszCaption, CWnd* pParentWnd, UINT iSelectPage)
	:CPropertySheet(pszCaption, pParentWnd, iSelectPage)
{
	InitPropPages();
}

CTrainPropertySheet::~CTrainPropertySheet()
{
	if (NULL != m_ppTech) { delete m_ppTech; m_ppTech = NULL; }
	if (NULL != m_ppShootSet) { delete m_ppShootSet; m_ppShootSet = NULL; }
}


BEGIN_MESSAGE_MAP(CTrainPropertySheet, CPropertySheet)
	//{{AFX_MSG_MAP(CTrainPropertySheet)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CTrainPropertySheet message handlers


BOOL CTrainPropertySheet::OnInitDialog() 
{
	// TODO: Add your specialized code here
	m_bModeless = FALSE;
	BOOL bResult = CPropertySheet::OnInitDialog();
	HWND btnOK = ::GetDlgItem(GetSafeHwnd(), IDOK);
	if (NULL != btnOK)
	{
		::SetWindowText(btnOK, "OK");
	}
	HWND btnCancel = ::GetDlgItem(GetSafeHwnd(), IDCANCEL);
	if (NULL != btnCancel)
	{
		::SetWindowText(btnCancel, "Cancel");
	}
	HWND btnApply = ::GetDlgItem(GetSafeHwnd(), ID_APPLY_NOW);
	if (NULL != btnApply)
	{
		::SetWindowText(btnApply, "Apply");
	}

	m_bModeless = TRUE;
	
	return bResult;
}

BOOL CTrainPropertySheet::PreTranslateMessage(MSG* pMsg) 
{
	// TODO: Add your specialized code here and/or call the base class
	switch (pMsg->message)
	{
	case WM_KEYDOWN:
		switch (pMsg->wParam)
		{
		case VK_ESCAPE:
		case VK_RETURN:
			::SendMessage(GetParent()->GetSafeHwnd(), WM_KEYDOWN, pMsg->wParam, pMsg->lParam);
			return TRUE;
		case VK_NEXT:
			{
				if(GetPageCount() > 1)
				{
				   if(GetActiveIndex() == 0)
					  SetActivePage(GetPageCount() - 1);
				   else
					  SetActivePage(GetActiveIndex() - 1);
				}
			}
			break;
		case VK_PRIOR:
			{
				if(GetPageCount() > 1)
				{
				   if(GetActiveIndex() == GetPageCount() - 1)
					  SetActivePage(0);
				   else
					  SetActivePage(GetActiveIndex() + 1);
				}
			}
			break;
		}
	}

	return CPropertySheet::PreTranslateMessage(pMsg);
}

BOOL CTrainPropertySheet::OnCommand(WPARAM wParam, LPARAM lParam)
{
	// allow message map override
    if (CWnd::OnCommand(wParam, lParam))
        return TRUE;

    // crack message parameters
    UINT nID = LOWORD(wParam);
    HWND hWndCtrl = (HWND)lParam;
    int nCode = HIWORD(wParam);

    // set m_nModalResult to ID of button, whenever button
    // is clicked
    if (hWndCtrl != NULL && nCode == BN_CLICKED)
    {
        if (::SendMessage(hWndCtrl, WM_GETDLGCODE, 0, 0) &
            (DLGC_BUTTON|DLGC_DEFPUSHBUTTON))
        {
            LONG lStyle = ::GetWindowLong(hWndCtrl, GWL_STYLE)
                                          & 0x0F;
            if (lStyle == BS_PUSHBUTTON || lStyle
                       == BS_DEFPUSHBUTTON ||
                lStyle == BS_USERBUTTON || lStyle == BS_OWNERDRAW)
            {
                if (nID == IDOK)
                {
					if (!m_ppTech->UpdateData() && !m_ppShootSet->UpdateData())
					{
						TRACE0("CPropertySheet:\tUpdateData error.\n");
						return FALSE;
					}
					TRACE0("CPropertySheet:\tUpdateData succeeded.\n");
					m_ppTech->DestroyWindow();
					m_ppShootSet->DestroyWindow();
					::SendMessage(GetParent()->GetSafeHwnd(), WM_CLOSE, 0, 0L);
					return TRUE;
                }
                else if (nID == ID_APPLY_NOW)
                {
                    CPropertyPage* activePage = GetActivePage();
					if (!activePage->UpdateData())
					{
						TRACE0("CPropertySheet:\tUpdateData error.\n");
						return FALSE;
					}
					TRACE0("CPropertySheet:\tUpdateData succeeded.\n");
					g_set_shooter2(m_ppTech->m_iLine, m_ppShootSet->m_dSTDist,
									m_ppShootSet->m_nMagRate);
                }
                else if (nID == IDCANCEL)
                {
					m_ppTech->DestroyWindow();
					m_ppShootSet->DestroyWindow();
                    ::SendMessage(GetParent()->GetSafeHwnd(), WM_CLOSE, 0, 0L);
					return TRUE;
                }
            }
        }
    }
    return FALSE;
}
